// mircoApp/cultivateCourse/myCourse/index.js
const app = getApp();
const skinBehavior = require('../../../utils/themeSkin/skinBehavior');

Page({
  behaviors: [skinBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    imgPath: app.globalData.imgPath,
    active: '0',
    show: false,
    donationUser: '',
    donationNum: 1,
    changingShow: false,
    notificationShow: false,
    donationShowItem: [],
    selectChanging_index: '',

    listData: [],
    isLoadMore: false,
    total_page: 0,
    page: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      active: '0'
    })
    this.init();
  },
  init() {
    this.setData({
      listData: [],
      isLoadMore: false,
      total_page: 0,
      page: 1,
    })
  },
  donationRecord() {
    wx.navigateTo({
      url: '/mircoApp/cultivateCourse/manage/donationRecord/donationRecord'
    })
  },
  notification_Tap(e) {
    let {
      item
    } = e.currentTarget.dataset;
    // console.log(item.course_term.notice_img)
    this.setData({
      notificationShow: true,
      notice_img: item.course_term.notice_img
    })
  },
  close_notificationShow() {
    this.setData({
      notificationShow: false,
      notice_img: ""
    })
  },
  previewImg() {
    wx.previewImage({
      current: this.data.notice_img, // 当前显示图片的http链接
      urls: this.data.notice_img // 需要预览的图片http链接列表
    });
  },
  changeTab(e) {
    let {
      index
    } = e.detail;
    this.setData({
      active: index
    })
    this.init();
    this.getData();
  },
  toCode(e) {
    let {
      item
    } = e.currentTarget.dataset;
    wx.navigateTo({
      url: '/mircoApp/cultivateCourse/chargeDetail/chargeDetail?id=' + item.id,
    })
  },
  stepperChange(e) {
    this.setData({
      donationNum: e.detail
    })
  },
  apply(e) {
    let {
      item
    } = e.currentTarget.dataset;
    if (item.course_term_id == 0) {
      wx.navigateTo({
        url: '/mircoApp/cultivateCourse/myCourse/applyCouse/applyCouse?gid=' + item.goods_id,
      })
      return
    }
    // item.course_term_id 不等于0直接去详情页面
    wx.navigateTo({
      url: '/mircoApp/cultivateCourse/courseClass/courseClassDetail/courseClassDetail?id=' + item.course_term_id,
    })

  },

  donationShow(e) {
    let item = e.currentTarget.dataset.item;
    this.setData({
      show: true,
      donationShowItem: item
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getData();
    let basic_info = wx.getStorageSync('yz_basic_info');
    wx.setNavigationBarTitle({
      title: basic_info.plugin_setting?.cultivate_course?.page_my_title,
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      wx.showLoading({
        title: '加载更多',
      })
      this.getMoreData();
    } else {
      console.log('没有更多了');
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  onClose() {
    this.setData({
      show: false,
      changingShow: false,
      donationShowItem: [],
      donationUser: '',
      selectChanging_data: [],
      course_list: [],
      donationUser_info: ""
    })
  },
  // 选择改签课程
  selectChanging(e) {
    let {
      item
    } = e.currentTarget.dataset;
    this.setData({
      selectChanging_data: item
    })
  },
  tapChangingMore(e) {
    app._postNetWork({
      url: app.getNetAddresss("plugin.cultivate-course.frontend.controllers.member.wait-quota.get-course-list"),
      data: {
        goods_id: this.data.course_list.goods_id,
        page: this.data.course_list.current_page + 1
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          this.data.course_list.data = this.data.course_list.data.concat(res.data.course_list.data)
          this.data.course_list.current_page = res.data.course_list.current_page;
          this.setData({
            'course_list.data': this.data.course_list.data,
            'course_list.current_page': this.data.course_list.current_page
          })
        } else {
          app.tips(res.msg);
        }
      }
    });
  },
  // 获取课程
  changingGetList(e) {
    let {
      item
    } = e.currentTarget.dataset;
    let json = {
      goods_id: item.quota_record.goods_id
    }
    app._postNetWork({
      url: app.getNetAddresss("plugin.cultivate-course.frontend.controllers.member.wait-quota.get-course-list"),
      data: json,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          this.setData({
            changingShow: true,
            course_list: res.data.course_list
          })
          this.data.course_list.id = item.id;
          // 设置默认效果
          this.setData({
            "selectChanging_data.id": item.course_term_id
          })
        } else {
          app.tips(res.msg);
        }
      }
    });
    console.log(item)
  },
  // 改签
  changing() {
    if (this.data.selectChanging_data && !this.data.selectChanging_data.id) {
      app.tips("请选择改签课程")
      return
    }
    let json = {
      id: this.data.course_list.id,
      course_term_id: this.data.selectChanging_data.id
    }
    app._postNetWork({
      url: app.getNetAddresss("plugin.cultivate-course.frontend.controllers.member.wait-clerk.change"),
      data: json,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          app.tips(res.msg);
          this.onClose();
          this.init();
          this.getData();
        } else {
          app.tips(res.msg);
        }
      }
    });

  },
  // 取消报名
  cancelApply(e) {
    let {
      item
    } = e.currentTarget.dataset;
    app._postNetWork({
      url: app.getNetAddresss("plugin.cultivate-course.frontend.controllers.member.wait-clerk.cancel-enroll"),
      data: {
        id: item.id
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          app.tips(res.msg);
          this.init();
          this.getData();
        } else {
          app.tips(res.msg);
        }
      }
    });
  },
  // 确认转赠
  submitDonation() {
    if (!this.data.donationUser_info) {
      app.tips('请选择受赠人')
      return
    }
    let urlStr = app.getNetAddresss("plugin.cultivate-course.frontend.controllers.member.wait-quota.transfer");
    let json = {
      goods_id: this.data.donationShowItem.goods_id,
      course_type: this.data.donationShowItem.course_type,
      count: this.data.donationNum,
      receive_member_id: this.data.donationUser_info.uid
    }
    app._postNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          app.tips(res.msg)
          this.setData({
            donationShowItem: [],
            donationNum: 1,
            donationUser_info: {},
            donationUser: '',
            show: false,
            page: 1,
            total_page: 0,
            isLoadMore: true
          })
          this.getData();
        } else {
          app.tips(res.msg)
        }
      }
    });
  },
  // 获取会员资料
  donationBlur() {
    if (!this.data.donationUser) {
      return;
    }
    let urlStr = app.getNetAddresss("plugin.cultivate-course.frontend.controllers.member.wait-quota.get-member");
    app._postNetWork({
      url: urlStr,
      data: {
        keyword: this.data.donationUser
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          if (!res.data.member) {
            app.tips('没有此会员')
            return
          }
          this.setData({
            donationUser_info: res.data.member,
          })
        } else {
          app.tips(res.msg)
        }
      }
    });
  },
  getData() {
    let urlStr
    if (this.data.active == 0) {
      urlStr = app.getNetAddresss("plugin.cultivate-course.frontend.controllers.member.wait-quota");
    } else if (this.data.active == 1) {
      urlStr = app.getNetAddresss("plugin.cultivate-course.frontend.controllers.member.wait-clerk.index");
    } else if (this.data.active == 2) {
      urlStr = app.getNetAddresss("plugin.cultivate-course.frontend.controllers.member.finish");
    }
    app._postNetWork({
      url: urlStr,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          this.data.isLoadMore = true;
          this.data.total_page = res.data.last_page;
          if (!this.data.total_page) {
            this.data.total_page = 0;
          }
          this.setData({
            listData: res.data.data
          })
        } else {
          app.tips(res.msg)
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  getMoreData() {
    let urlStr = "";
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      wx.hideLoading();
      return;
    } else {
      this.setData({
        page: this.data.page + 1
      })
      if (this.data.active == 0) {
        urlStr = app.getNetAddresss("plugin.cultivate-course.frontend.controllers.member.wait-quota");
      } else if (this.data.active == 1) {
        urlStr = app.getNetAddresss("plugin.cultivate-course.frontend.controllers.member.wait-clerk.index");
      } else if (this.data.active == 2) {
        urlStr = app.getNetAddresss("plugin.cultivate-course.frontend.controllers.member.finish");
      }
      app._postNetWork({
        url: urlStr,
        data: {
          page: this.data.page
        },
        success: (resdata) => {
          let res = resdata.data
          if (res.result == 1) {
            this.data.isLoadMore = true;
            this.setData({
              listData: this.data.listData.concat(res.data.data)
            })
            wx.hideLoading();
          } else {
            wx.hideLoading();
            this.data.page = this.data.page - 1;
            this.isLoadMore = false;
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  }
})